﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.PowerPacks;
using System.Windows.Forms;
using System.Drawing;

namespace BattleshipGameClient
{
    public class Board
    {
        private Grid grid;
        // Grid size in number of cell. 
        int gridWidth;
        public int GridWidth
        {
          get { return gridWidth; }
          set { gridWidth = value; }
        }
        int gridHeight;
        public int GridHeight
        {
          get { return gridHeight; }
          set { gridHeight = value; }
        }

        // Cell size in pixel (square cell).
        int cellSize;
        public int CellSize
        {
            get { return cellSize; }
            set { cellSize = value; }
        }

        // Constructor
        public Board(int gridWidth, int gridHeight, int cellSize, Point offset)
        {
            grid = new Grid(gridWidth, gridHeight, cellSize, offset);
            GridWidth = gridWidth;
            GridHeight = gridHeight;
            CellSize = cellSize;
        }

        // Accessors
        public Dictionary<string, Cell> getCellDictionary()
        {
            return grid.cellDictionary;
        }

        public ShapeContainer getGraphGrid()
        {
            return grid.graphGrid;
        }

        public void setGraphGridParent(Form parent)
        {
            grid.graphGrid.Parent = parent;
            //foreach (KeyValuePair<string, Board.Cell> pair in this.getCellDictionary())
            //{
            //    pair.Value.GraphCell.Parent = grid.graphGrid;
            //}
        }

        public void setGraphCellTexture(Image cellTexture)
        {
            foreach (KeyValuePair<string, Board.Cell> pair in this.getCellDictionary())
            {
                pair.Value.GraphCell.BackgroundImage = cellTexture;
            }
        }

        /// <summary>
        /// The graphic representation of the board is a grid.
        /// </summary>
        public class Grid
        {
            public Dictionary<string, Cell> cellDictionary;
            public ShapeContainer graphGrid;

            // Grid size in number of cell.
            public Grid(int gridWidth, int gridHeight, int cellSize, Point offset)
            {
                cellDictionary = new Dictionary<string, Cell>();
                graphGrid = new ShapeContainer();
                graphGrid.Location = offset;

                // Create all the Cell.
                for (int j = 0; j < gridHeight; j++)
                {
                    for (int i = 0; i < gridWidth; i++)
                    {
                        // The format for the coordinate is : "height;width".
                        int height = j+1; int width= i+1;
                        string coordinate = height.ToString() + ";" + width.ToString();
                        // Add the new Cell created to the dictionnary.
                        cellDictionary.Add(coordinate, new Cell(cellSize, i, j, graphGrid));
                    }
                }
            }
        }

        /// <summary>
        /// A grid contains cells.
        /// </summary>
        public class Cell
        {
            // Class for holding cell status information.
            public enum cellState
            {
                WATER,
                SHIPUNIT,
                SCAN,                
                MISS,
                HIT,
                SINK
            }

            private cellState currentCell;
            public cellState CurrentCell
            {
                get { return currentCell; }
                set { currentCell = value; }
            }

            private RectangleShape graphCell;
            public RectangleShape GraphCell
            {
                get { return graphCell; }
                set { graphCell = value; }
            }

            public Cell(int cellSize, int columnID, int rowID, ShapeContainer graphGrid)
            {
                currentCell = cellState.WATER;
                // Create and configure the new graphical representation of the Cell.
                graphCell = new RectangleShape(graphGrid);
                setGraphCell(cellSize, columnID, rowID);
            }

            public Cell(int cellSize, cellState CellState)
            {
                currentCell = CellState;
            }

            private void setGraphCell(int cellSize, int columnID, int rowID)
            {
                //graphCell = new Rectangle(columnID*cellSize, rowID*cellSize, cellSize, cellSize);
                int x = graphCell.Parent.Location.X + columnID * cellSize;
                int y = graphCell.Parent.Location.Y + rowID * cellSize;
                graphCell.Location = new Point(x, y);
                graphCell.Size = new Size(cellSize, cellSize);
                graphCell.Enabled = true;
                graphCell.Name = (rowID + 1).ToString() + ";" + (columnID + 1).ToString();
            }
        }

    }
}
